//
//  Details.m
//  YiLiao
//
//  Created by mac on 16/7/2.
//  Copyright © 2016年 Noah. All rights reserved.
//

#import "Details.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "DetailsFirst.h"
#import "DetailsSecond.h"
#import "DetailThree.h"
#import "DetailFiveCell.h"
@interface Details ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray  *allValueDdta;
    NSDictionary *allValueInfo;
    NSArray *allValueCom;
    
}
@property (nonatomic ,strong)UITableView *tableView;
@end

@implementation Details

-(UITableView *)tableView{
    if (!_tableView) {
        self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth, kScreenHeight-20) style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
       
        //标题
        [_tableView registerNib:[UINib nibWithNibName:@"DetailsFirst" bundle:nil] forCellReuseIdentifier:@"cellF"];
        //图片
        [_tableView registerNib:[UINib nibWithNibName:@"DetailsSecond" bundle:nil] forCellReuseIdentifier:@"cellS"];
        //评论
        [_tableView registerNib:[UINib nibWithNibName:@"DetailThree" bundle:nil] forCellReuseIdentifier:@"cellT"];
        //文字
        [_tableView registerNib:[UINib nibWithNibName:@"DetailFiveCell" bundle:nil] forCellReuseIdentifier:@"cellFI"];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];

 
    [self.view addSubview:self.tableView];
    [self getData];
}
-(void)getData{
 
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    manager .responseSerializer.acceptableContentTypes=[manager .responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/plain", nil]];
    [manager GET:[NSString stringWithFormat:@"http://localhost:8080%@",self.soureURL] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *infoDic=(NSDictionary *)responseObject;

        allValueInfo=infoDic[@"info"];
       
        allValueDdta=infoDic[@"data"];
        allValueCom=infoDic [@"comments"];
         NSLog(@"%@%@%@",allValueInfo,allValueCom,allValueDdta);
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}
//区头
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
//
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    if (section==0) {
//        return nil;
//    }else if(section==1){
//        return nil;
//    }else if(section==1){
//        return  @"热门跟帖";
//    }
//    return @"";
    if (section==2) {
        return @"热门跟帖";
    }
        return nil;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 200;
        
    }else if (indexPath.section==1) {
        return 200;
    }
        return 120;
    }
    

//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }else if (section==1){
        return allValueDdta.count;
    }else{
         return allValueCom.count;
    }
   
}
//
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section==0) {
      
            DetailsFirst *cellF=[tableView dequeueReusableCellWithIdentifier:@"cellF" forIndexPath:indexPath];
            
            
            cellF.time.text=allValueInfo[@"time"];
            cellF.title.text=allValueInfo[@"news_title"];
            cellF.source.text=allValueInfo[@"source"];
            cellF.intro.text=allValueInfo[@"intro"];
            cellF.autro.text=allValueInfo[@"auther"];
            return cellF;
        }else if(indexPath.section==1){
            if ( [allValueDdta [indexPath.row] [@"data_type"] isEqualToString:@"2"] ) {
                
                DetailsSecond *cellS=[tableView dequeueReusableCellWithIdentifier:@"cellS" forIndexPath:indexPath];
                NSDictionary *dic=allValueDdta[indexPath .row];
                NSDictionary *dic1=dic[@"image"];
                NSArray *array=dic1[@"source"];
                [cellS.dataImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080%@",array]]];
                return cellS;
                
                
            }else {
        
                DetailFiveCell *cellI=[tableView dequeueReusableCellWithIdentifier:@"cellFI" forIndexPath:indexPath];
                NSDictionary *dic=allValueDdta[indexPath .row];
                cellI.contextLabel.text=dic[@"content"];
                return cellI;
            }
        }else {
        DetailThree *cellT=[tableView dequeueReusableCellWithIdentifier:@"cellT" forIndexPath:indexPath];
        NSDictionary *dic2=allValueCom[indexPath.row];
        cellT.contece.text=dic2[@"info"];
        cellT.nameLabel.text=dic2[@"name"];
        return cellT;
        }
  
}

@end
